package cn.edu.scau.cmi.chensicheng.client;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.chensicheng.composite.consistent.ConsistentComponent;
import cn.edu.scau.cmi.chensicheng.composite.consistent.ConsistentComposite;
import cn.edu.scau.cmi.chensicheng.composite.consistent.ConsistentLeaf;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;

public class CompositeClient extends Application implements Initializable{
	@FXML
	private Button button1;
	@FXML
	private Button button2;
	@FXML
	private Button button3;
	@FXML
	private Button button4;
	@FXML
	private TextArea textArea;
	private ArrayList<String> persons= new ArrayList<String>() {{
		add("张三");
		add("李四");
		add("王五");
		add("徐六");
		add("田七");
		add("刘翔");
		add("姚明");
		add("张飞");
	}};
	private int personIndex=0;
	private ArrayList<String> organizations= new ArrayList<String>() {{
		add("红十字会");
		add("ACM协会");
		add("CCF协会");
		add("NBA协会");
		add("CBA协会");
		add("吉他协会");
		add("钢琴协会");
		add("提琴协会");
	}};
	private int organizationIndex=0;
	
	@Override
	public void start(Stage primaryStage) throws IOException {
		Scene scene=new Scene(FXMLLoader.load(getClass().getResource("../Composite.fxml")));
		primaryStage.setScene(scene);
		primaryStage.setTitle("实验六组合模式");
		primaryStage.show();
	}

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		ConsistentComposite cc=new ConsistentComposite("初始组织");
		button1.setOnAction(e->{
			textArea.setText(cc.addChild(new ConsistentComposite(organizations.get((organizationIndex++)%8))));
		});
		button2.setOnAction(e->{
			textArea.setText(cc.addChild(new ConsistentLeaf(persons.get((personIndex++)%8))));
		});
		button3.setOnAction(e->{
			textArea.setText(cc.deleteChild(0));
		});
		button4.setOnAction(e->{
			textArea.setText("");
			for(ConsistentComponent component : cc.getChildren()) {
				textArea.appendText(component.name+'\n');
			}
		});
	}
}
